﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Zyg.SSO.Core.Business;
using System.Collections.Generic;

namespace Zyg.SSO.Web
{
    public partial class APList : BasePage
    {
        public List<App> applist;
        public List<App> otherAppList;
        public int i = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            applist = handleAPPList();

            IList<App> allApps = App.GetAllApps();

            otherAppList = new List<App>();

            foreach (App app in allApps)
            {
                bool owned = false;
                foreach (App myApp in applist)
                {
                    if (myApp == app) { owned = true; break; }
                }

                if (!owned) { otherAppList.Add(app); }
            }

            HandleAppPictureAddress(applist);
            HandleAppPictureAddress(otherAppList);
        }

        List<App> handleAPPList()
        {
            Account user = base.Account;

            List<App> tlist = new List<App>();
            int iCount = user.Apps.Count;
            for (int j = 0; j < iCount; ++j)
            {
                tlist.Add(user.Apps[j].App);
            }
            return tlist;
        }

        protected void HandleAppPictureAddress(IList<App> apps)
        {
            for (int i = apps.Count - 1; i > -1; i--)
            {
                if (!string.IsNullOrEmpty(apps[i].PictureAddress))
                {
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath(apps[i].PictureAddress));

                    if (!fileInfo.Exists)
                    {
                        apps[i].PictureAddress = Server.MapPath("~/Content/App/app.jpg");
                    }
                    else
                    {
                        apps[i].PictureAddress = ResolveUrl(apps[i].PictureAddress);
                    }
                }
                else
                {
                    apps[i].PictureAddress = "/Content/App/app.jpg";
                }
            }
        }
    }
}
